package com.jsecode.protocol.gid22;

import org.apache.commons.lang.builder.ToStringBuilder;

import io.netty.buffer.ByteBuf;

public abstract class GidBody implements IGidBody {

    /** 保存pack后的结果，便于后续获取 */
    protected ByteBuf packedBuf;
    /** body转成字节数组的长度，用于接写数据后从头传入 */
    protected int len;

    public GidBody(int len){
        this.len = len;
    }

    public GidBody(){
    }

    @Override
    public ByteBuf pack(){
        return null;
    }

    @Override
    public int unpack(ByteBuf srcBuf){
        return -1;
    }

    @Override
    public byte[] getBytes(){
        if (packedBuf == null) {
            packedBuf = pack();
        }
        if (packedBuf != null) {
            return packedBuf.array();
        } else {
            return null;
        }
    }

    public int getLen(){
        return len;
    }

    public void setLen(int len){
        this.len = len;
    }

    @Override
    public String toString(){
        return ToStringBuilder.reflectionToString(this);
    }
}
